import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import axios from "axios";
import {store, ajax} from "./core";
import "./directives";
import ElementUI from "element-ui";
import "babel-polyfill";
import "./mock/index.js";

import "./utils/dataGridResize";

import "element-ui/lib/theme-default/index.css";
import "./static/css/main.scss";
import "font-awesome/css/font-awesome.min.css";

Vue.use(ElementUI);

axios.defaults.baseURL = 'http://106.14.157.177:58560';

//axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';//配置请求头

//返回状态判断(添加响应拦截器)
axios.interceptors.response.use((res) =>{
  //对响应数据做些事
  if(!res.data.successed){
      ElementUI.MessageBox.alert(res.data.message);
      return Promise.reject(res);
  }
  return res;
}, (error) => {
    _.toast("网络异常", 'fail');
    return Promise.reject(error);
});

Vue.prototype.$axios = axios;
Vue.prototype.$ = ajax;

const vm = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");

Vue.prototype.$.vm = vm;
Vue.prototype.$axios.vm = vm;

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!localStorage.getItem("user")) {
      next({
        path: "/login",
        query: {redirect: to.fullPath}
      });
    } else {
      next();
    }
  } else {
    next();
  }
});
